package com.itboye.ihomebank.widget.choosecity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.ChooseCityListAdapter;
import com.itboye.ihomebank.db.DBManager;

import java.util.ArrayList;

/**
 * 城市搜索列表
 *
 */
public class ChooseCityActivity extends BaseOtherActivity {
	private ImageView close_icon;// 返回图片
	private TextView add_shap_title_tv;// 标题
	EditText edit_inputcity;
	ArrayList<String> arrayStrings = new ArrayList<>();
	private SQLiteDatabase database;
	protected Cursor cursor;
	ListView listview;
	ChooseCityListAdapter adapter;
	private DBManager dbManager;
	ImageView delete;

	public ArrayList<String> queryInfo(Cursor cursor) {
		try {
			while (cursor.moveToNext()) {
				arrayStrings.add(cursor.getString(cursor
						.getColumnIndex("CityName")));
			}

		} catch (Exception e) {
			// TODO: handle exception
			e.toString();
		} finally {
			// database.close();
		}

		return arrayStrings;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.close_icon:
			finish();
			break;

		default:
			break;
		}
		
	}

	@Override
	protected int layout_id() {
		// TODO Auto-generated method stub
		return R.layout.activity_choosecity;
	}

	@Override
	protected void onInitController() {
		// TODO Auto-generated method stub
		
	}

	@SuppressLint("ResourceAsColor") @Override
	protected void onInit() {
		add_shap_title_tv.setText("城市搜索");
		setStatusBarColor(R.color.home_blue);
		adapter = new ChooseCityListAdapter(this, arrayStrings);
		listview.setAdapter(adapter);
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				edit_inputcity.setText(arrayStrings.get(position));
				Intent data=new Intent();
				data.putExtra("city", arrayStrings.get(position));
				setResult(102,data);
				finish();
			}
		});
		dbManager = new DBManager(this);
		dbManager.openDateBase();
		database = SQLiteDatabase.openOrCreateDatabase(DBManager.DB_PATH + "/"
				+ DBManager.DB_NAME, null);
		delete.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				edit_inputcity.setText("");
			}
		});
		edit_inputcity.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub
				if(s.length()>0){
					delete.setVisibility(View.VISIBLE);
				}else
				{
					delete.setVisibility(View.GONE);
				}
			//	arrayStrings = new ArrayList<String>();
				// 创建模糊查询的条件
				String content = edit_inputcity.getText().toString();
				if (!TextUtils.isEmpty(content)) {
					dbManager = new DBManager(ChooseCityActivity.this);
					String likeStr = "'";
					for (int i = 0; i < content.length(); i++) {
						if (i < content.length() - 1) {
							likeStr += "%" + content.charAt(i);
						} else {
							likeStr += "%" + content.charAt(i) + "%'";
						}
					}
					cursor = database.rawQuery(("select * from " + "T_city"
							+ " where CityName like " + likeStr), null);
					arrayStrings = queryInfo(cursor);
					adapter.refreshData(arrayStrings);
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub

			}
		});
	}

	@Override
	protected void onReadIntent(Bundle bundle) {
		// TODO Auto-generated method stub
		
	}

	
}
